package com.sntk.organauthomanager.application.controller;

import com.alibaba.fastjson.JSON;
import com.sntk.organauthomanager.application.dto.*;
import com.sntk.organauthomanager.application.service.OrganizationApplicationService;
import com.sntk.organauthomanager.infrastructure.autho.PassToken;
import com.sntk.organauthomanager.infrastructure.session.LoginSessionInfo;
import com.sntk.organauthomanager.infrastructure.validation.Insert;
import com.sntk.organauthomanager.infrastructure.validation.Update;
import com.sntk.organauthomanager.infrastructure.validation.Validation;
import com.sntk.organauthomanager.query.OrganizationQuery;
import com.sntk.organauthomanager.query.param.OrganCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: OrganizationController
 * @author: wgchao
 * @createTime: 2020/7/20 5:05 PM
 * @Description: TODO
 */
@RestController
@RequestMapping("/sinohis/osm/external/sys/organization")
@Api(value="OrganizationController",tags={"组织机构管理"})
public class OrganizationController {
    @Autowired
    private OrganizationApplicationService organizationApplicationService;
    @Autowired
    private OrganizationQuery organizationQuery;

    @PostMapping
    @ApiOperation(value="创建组织机构")
    public ResponseData createOrganization(@ApiParam(name="creator",value="组织机构信息",required=true)
                                               @RequestBody @Validated(value = {Insert.class}) OrganizationCreator creator, BindingResult result, HttpSession httpSession){
        List<?> l = Validation.getValidation(result);
        if(l.size() != 0) {
            return ResponseData.error(JSON.toJSONString(l));
        }
        organizationApplicationService.createOrganization(creator, LoginSessionInfo.generate(httpSession).getOperator());
        return ResponseData.success();
    }

    @PutMapping
    @ApiOperation(value="修改机构信息")
    public ResponseData modifyOrganInfo(@ApiParam(name="organizationDTO",value="组织机构信息",required=true)
                                        @RequestBody @Validated(value = {Update.class}) OrganizationDTO organizationDTO, BindingResult result, HttpSession httpSession){
        List<?> l = Validation.getValidation(result);
        if(l.size() != 0) {
            return ResponseData.error(JSON.toJSONString(l));
        }
        organizationApplicationService.modifyOrganInfo(organizationDTO, LoginSessionInfo.generate(httpSession).getOperator());
        return ResponseData.success();
    }

    @DeleteMapping("/{institutionNumber}")
    @ApiOperation(value="删除机构")
    public ResponseData removeOrgan(@ApiParam(name="institutionNumber",value="组织机构编号",required=true)
                                        @PathVariable @Validated(value = {Update.class}) String institutionNumber, HttpSession httpSession){
        organizationApplicationService.removeOrgan(institutionNumber, LoginSessionInfo.generate(httpSession).getOperator());
        return ResponseData.success();
    }

    @GetMapping("/page/condition")
    @ApiOperation(value="分页条件查询机构信息")
    public ResponseData<PageData<OrganizationVO>> searchOrgansOfConditions(@ApiParam(name="organCondition",value="分页查询条件",required=true)
                                                                                   OrganCondition organCondition, HttpSession httpSession){
        PageData<OrganizationVO> voPageData = organizationQuery.searchOrgansOfConditions(organCondition, LoginSessionInfo.generate(httpSession).getOperatorInfo());
        return ResponseData.success().setData(voPageData);
    }

    @GetMapping
    @ApiOperation(value="查询机构信息")
    public ResponseData<OrganizationDTO> searchOrgan(@ApiParam(name="institutionNumber",value="机构编号",required=true) String institutionNumber){
        OrganizationDTO organizationDTO = organizationQuery.searchOrgan(institutionNumber);
        return ResponseData.success().setData(organizationDTO);
    }
    @GetMapping("/searchOrganChilds/{institutionNumber}")
    @ApiOperation(value="查询机构以及子级机构信息")
    public ResponseData searchOrganChilds(@ApiParam(name="institutionNumber",value="机构编号",required=true)
                                          @PathVariable String institutionNumber){

        List<OrganizationVO> organizationVOList = organizationQuery.searchOrganChilds(institutionNumber);
        return ResponseData.success().setData(organizationVOList);
    }

    @GetMapping("/searchOrganOfUserAffiliate/{uname}")
    @ApiOperation(value="查询用户关联机构")
    public ResponseData<List<OrganizationDTO>> searchOrganOfUserAffiliate(@ApiParam(name="uname",value="账户号",required=true)
                                          @PathVariable String uname){

        List<OrganizationDTO> organizationDTOS = organizationQuery.findUserAffiliates(uname);
        return ResponseData.success().setData(organizationDTOS);
    }

    @GetMapping("/medical/gworgan")
    @ApiOperation(value="查询可分配gw的机构信息")
    public ResponseData<List<OrganizationDTO>> searchUserOfOrgan(){
        List<OrganizationDTO> dtos = organizationQuery.searchUnallocatedGWOrgans();
        return ResponseData.success().setData(dtos);
    }


    @GetMapping(value = {"/medical/organsetting", "/medical/organsetting/{institutionName}"})
    @ApiOperation(value="查询未进行机构设置的机构列表")
    @PassToken
    public ResponseData<List<OrganizationDTO>> searchUnallocatedOrgansSetting(@PathVariable(required = false) String institutionName){
        List<OrganizationDTO> dtos = organizationQuery.searchUnallocatedOrgansSetting(institutionName);
        return ResponseData.success().setData(dtos);
    }


}
